package com.tuling.proxy;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy {
    public static void main(String[] args) {
        UserServiceClass userServiceClass  = new UserServiceClass();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserServiceClass.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

                System.out.println("before");
                method.invoke(userServiceClass,objects);

                System.out.println("after");
                return null;
            }
        });

        UserServiceClass target = (UserServiceClass)enhancer.create();
        target.test();
    }
}
